"""
Case Type   : bugfix
Case Name   : 兼容B库下，关闭ansi_quotes，alter table 修改列名含大写的表，修改成功
Create At   : 2025/6/11
Owner       : liu-tong-8848
Description :
    1、关闭ansi_quotes
    2、建表列名含大写
    3、查看表结构
    4、alter table修改列名含大写的列为自增列
    5、恢复环境
Expect      :
    1、成功
    2、成功
    3、成功
    4、成功
    5、成功
History     :
"""

import os
import unittest

from yat.test import Node
from yat.test import macro

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class BugFix(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.common = Common()
        self.sh_primary = CommonSH('PrimaryDbBmode')
        self.user_node = Node('PrimaryDbBmode')
        self.constant = Constant()
        self.pg_file = macro.DB_INSTANCE_PATH + "/" + "postgresql.conf"
        sql_cmd = f"show dolphin.sql_mode;"
        self.log.info(sql_cmd)
        self.dolphin_sql_mode = self.sh_primary.execut_db_sql(sql_cmd).strip().splitlines()[-2]
        self.log.info(self.dolphin_sql_mode)

    def test_dolphin(self):
        self.log.info(f'----{os.path.basename(__file__)} start----')
        text = '----step1:关闭ansi_quotes；expect:成功----'
        self.log.info(text)
        echo_cmd = f"echo \"dolphin.sql_mode = ''\" >> {self.pg_file}"
        result = self.user_node.sh(echo_cmd)
        self.log.info(result)
        self.sh_primary.restart_db_cluster()
        sql_cmd = f"show dolphin.sql_mode;"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertNotIn('ansi_quotes', sql_res, '执行失败' + text)

        text = '----step2:建表列名含大写；expect:成功----'
        self.log.info(text)
        sql_cmd = "drop table if exists t_fixed_112;create table t_fixed_112(\`ABcD\` int, primary key(\`ABcD\`));"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS, sql_res, '执行失败' + text)
        self.assertIn(self.constant.DROP_TABLE_SUCCESS, sql_res, '执行失败' + text)

        text = '----step3:查看表结构；expect:无双引号引用字段----'
        self.log.info(text)
        sql_cmd = "show create table t_fixed_112;"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertNotIn('"', sql_res, '执行失败' + text)

        text = '----step4:alter table修改列名含大写的列为自增列；expect:成功----'
        self.log.info(text)
        sql_cmd = "alter table t_fixed_112 modify `ABcD` int auto_increment;"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn('ABcD', sql_res, '执行失败' + text)
        sql_cmd = "alter table t_fixed_112 modify `abcd` int auto_increment;"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn('abcd', sql_res, '执行失败' + text)
        sql_cmd = "alter table t_fixed_112 modify ABcD int auto_increment;"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn('ABcD', sql_res, '执行失败' + text)

    def tearDown(self):
        text = '----step5:清理环境；expect:成功----'
        self.log.info(text)
        sql_cmd = f"drop table if exists t_fixed_112;"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.assertIn(self.constant.DROP_TABLE_SUCCESS, sql_res, '执行失败' + text)
        echo_cmd = f"sed -i '/^dolphin.sql_mode/d' {self.pg_file}"
        result = self.user_node.sh(echo_cmd)
        self.log.info(result)
        self.sh_primary.restart_db_cluster()
        sql_cmd = f"show dolphin.sql_mode;"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn('ansi_quotes', sql_res, '执行失败' + text)
        self.log.info(f'----{os.path.basename(__file__)} end-----')
